package com.jwsoft.manager.core.dao.service.impl;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson2.JSON;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.cache.jetcache.utils.JetCacheUtils;
import com.bifang.module.base.core.helper.DictHelper;
import com.bifang.module.system.common.enums.ArticleStatusEnum;
import com.bifang.module.system.core.dao.model.AppArticle;
import com.bifang.module.system.core.dao.service.AppArticleService;
import com.jwsoft.manager.common.constant.EduCache;
import com.jwsoft.manager.common.enums.*;
import com.jwsoft.manager.common.vo.bigScreen.analysisXq.*;
import com.jwsoft.manager.common.vo.dataStatistics.school.SchoolEnrollCntVO;
import com.jwsoft.manager.common.vo.dataStatistics.school.SchoolEnrollQueryVO;
import com.jwsoft.manager.common.vo.dataStatistics.school.SchoolEnrollVO;
import com.jwsoft.manager.common.vo.eduEnrollmenBrochure.EduEnrollmenBrochureVO;
import com.jwsoft.manager.common.vo.eduEnrollmenBrochure.OtherAreaConfigNameVO;
import com.jwsoft.manager.common.vo.eduFormEnroll.EduFormEnrollVO;
import com.jwsoft.manager.common.vo.eduFormEnrollParams.EduFormEnrollParamsVO;
import com.jwsoft.manager.common.vo.eduSchool.EduSchoolVO;
import com.jwsoft.manager.common.vo.eduSchoolConfig.EduSchoolConfigVO;
import com.jwsoft.manager.core.dao.mapper.EduSchoolMapper;
import com.jwsoft.manager.core.dao.model.*;
import com.jwsoft.manager.core.dao.service.*;
import com.jwsoft.manager.core.util.EduHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 所属学校表服务实现类
 *
 * @author wangtao
 * @since 2022-11-25
 */
@Service
public class EduSchoolServiceImpl extends ServiceImpl<EduSchoolMapper, EduSchool> implements EduSchoolService {
    @Autowired
    DictHelper dictHelper;
    @Autowired
    EduHelper eduHelper;
    @Autowired
    EduAreaService eduAreaService;
    @Autowired
    AppArticleService appArticleService;
    @Autowired
    EduEnrollmenBrochureService eduEnrollmenBrochureService;
    @Autowired
    EduAppointSchoolService eduAppointSchoolService;
    @Autowired
    EduSchoolConfigService eduSchoolConfigService;
    @Autowired
    EduFormEnrollService eduFormEnrollService;
    @Autowired
    EduFormEnrollParamsService eduFormEnrollParamsService;
    @Autowired
    EduFormSpecialService eduFormSpecialService;
    @Autowired
    EduFormSpecialParamsService eduFormSpecialParamsService;
    @Autowired
    EduSchoolBbService eduSchoolBbService;
    @Autowired
    EduSchoolVacancyService eduSchoolVacancyService;
    @Autowired
    EduBreakThroughService eduBreakThroughService;
    @Autowired
    EduBreakThroughDetailService eduBreakThroughDetailService;

    @Override
    public List<EduSchoolVO> selectSchoolList(Map<String, Object> record) {
        return baseMapper.selectSchoolList(record);
    }

    @Override
    public List<EduSchoolVO> querySchoolStaInfo(Map<String, Object> record) {
        return baseMapper.querySchoolStaInfo(record);
    }

    @Override
    public List<EduSchoolVO> querySchoolStaInfoHistory(Map<String, Object> record) {
        return baseMapper.querySchoolStaInfoHistory(record);
    }
    @Override
    public List<SchoolEnrollVO> queryRegistStatistics(Map<String, Object> record) {
        return baseMapper.queryRegistStatistics(record);
    }

    @Override
    public List<SchoolEnrollCntVO> queryApplyNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryApplyNum(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryApplyYxNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryApplyYxNum(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryAuditWaitNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryAuditWaitNum(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryYeyApplyNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryYeyApplyNum(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryYeyAuditWaitNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryYeyAuditWaitNum(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryAuditNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryAuditNum(vo);
    }
    @Override
    public List<SchoolEnrollCntVO> queryYeyAuditNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryYeyAuditNum(vo);
    }
    @Override
    public List<SchoolEnrollCntVO> queryAdmitNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryAdmitNum(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryAdmitXxNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryAdmitXxNum(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryYeyAdmitNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryYeyAdmitNum(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryYeyApplyNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryYeyApplyNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryYeyAuditWaitNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryYeyAuditWaitNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryYeyAuditNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryYeyAuditNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryYeyAdmitNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryYeyAdmitNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollVO> queryBmByLb(SchoolEnrollQueryVO vo) {
        return baseMapper.queryBmByLb(vo);
    }

    @Override
    public List<SchoolEnrollVO> queryBmByLbHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryBmByLbHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryFromNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryFromNum(vo);
    }
    @Override
    public List<SchoolEnrollCntVO> queryToNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryToNum(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> querySqNum(SchoolEnrollQueryVO vo) {
        return baseMapper.querySqNum(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryFsqNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryFsqNum(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryTstdNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryTstdNum(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryBbNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryBbNum(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryQbNum(SchoolEnrollQueryVO vo) {
        return baseMapper.queryQbNum(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryApplyNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryApplyNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryApplyYxNumHistory(SchoolEnrollQueryVO vo) {
        return  baseMapper.queryApplyYxNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryAuditWaitNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryAuditWaitNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryAuditNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryAuditNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryAdmitNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryAdmitNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryAdmitXxNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryAdmitXxNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryFromNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryFromNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryToNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryToNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> querySqNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.querySqNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryFsqNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryFsqNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryTstdNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryTstdNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryBbNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryBbNumHistory(vo);
    }

    @Override
    public List<SchoolEnrollCntVO> queryQbNumHistory(SchoolEnrollQueryVO vo) {
        return baseMapper.queryQbNumHistory(vo);
    }

    @Override
    public List<AreaXwpmVO> getAreaXwpmVO(SchoolOverviewXqQueryVO vo) {
        return baseMapper.getAreaXwpmVO(vo);
    }
    @Override
    public List<SchoolXwpmVO> getSchoolXwpmVO(SchoolXwpmQueryVO vo) {
        return baseMapper.getSchoolXwpmVO(vo);
    }
    @Override
    public SchoolXqVO getSchoolXqVO(SchoolXqQueryVO vo) {
        return baseMapper.getSchoolXqVO(vo);
    }
    @Override
    public List<SchoolXqVO> getSchoolXqVOList(SchoolXqQueryVO vo) {
        return baseMapper.getSchoolXqVOList(vo);
    }

    @Override
    @Cached(name = EduCache.EDU_SCHOOL_DETAIL, key = "#orgId", cacheType = CacheType.BOTH, expire = 86400, localExpire = 20)
    public EduSchoolVO getDetail(String orgId) {
        //获取学校详情
        if(StringUtils.isBlank(orgId)){
            throw new AppException("学校id不能为空");
        }
        EduSchool entity = this.getById(orgId);
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        EduSchoolVO result = Convert.convert(EduSchoolVO.class, entity);
        dictHelper.valueToName(result, Arrays.asList(SchoolDictEnum.values()));
        if(StringUtils.isNotBlank(result.getAreaCode())){
            result.setAreaName(dictHelper.getAreaNameByCode(result.getAreaCode()));
        }
        if(StringUtils.isNotBlank(result.getStreet())) {
            QueryWrapper<EduArea> areaQueryWrapper = new QueryWrapper<>();
            areaQueryWrapper.lambda().eq(EduArea::getLocalCode, result.getStreet());
            List<EduArea> areaList = eduAreaService.list(areaQueryWrapper);
            if (!CollectionUtils.isEmpty(areaList)) {
                result.setStreetName(areaList.get(0).getAreaName());
            }
        }
        Integer year =eduHelper.thisYear();
        //判断是否城区E学校
        result.setAppointFlag(false);
        if(!SchoolCategoryEnum.KINDERGARTEN.getType().equalsIgnoreCase(result.getSchoolCategory())
                && !SchoolNatureEnum.mb.getType().equalsIgnoreCase(result.getSchoolNature())
                && !SchoolNatureEnum.mzg.getType().equalsIgnoreCase(result.getSchoolNature())){
            QueryWrapper<EduAppointSchool> appointSchoolQueryWrapper = new QueryWrapper<>();
            appointSchoolQueryWrapper.lambda().eq(EduAppointSchool::getNewSchoolId, orgId)
                    .eq(EduAppointSchool::getStatus, BoolEnum.TRUE.getType())
                    .eq(EduAppointSchool::getYear, year);
            if(eduAppointSchoolService.count(appointSchoolQueryWrapper)>0){
                result.setAppointFlag(true);
            }
        }
        List<String> areaEnrollList=new ArrayList<>();
        //获取招生方案
        QueryWrapper<EduEnrollmenBrochure> brochureQueryWrapper = new QueryWrapper<>();
        brochureQueryWrapper.lambda().eq(EduEnrollmenBrochure::getSchoolId, orgId)
                .eq(EduEnrollmenBrochure::getYear, year);
        EduEnrollmenBrochure brochure = eduEnrollmenBrochureService.getOne(brochureQueryWrapper,false);
        if(brochure==null){
            result.setHasEnrollFlag(false);
            result.setEnrollAreaConfigList(new ArrayList<>());
        }else{
            result.setHasEnrollFlag(false);
            EduEnrollmenBrochureVO brochureVO=Convert.convert(EduEnrollmenBrochureVO.class,brochure);
            if(brochureVO.getArticleId()!=null){
                QueryWrapper<AppArticle> queryWrapper=new QueryWrapper<>();
                queryWrapper.lambda().select(AppArticle::getArticleStatus).eq(AppArticle::getArticleId,brochureVO.getArticleId());
                AppArticle appArticle=appArticleService.getOne(queryWrapper,false);
                if(appArticle!=null){
                    brochureVO.setArticleStatus(appArticle.getArticleStatus());
                    if(ArticleStatusEnum.on.getType().equalsIgnoreCase(appArticle.getArticleStatus())){
                        result.setHasEnrollFlag(true);
                    }
                }
            }
            result.setZsContacts(brochureVO.getContacts());
            result.setZsPhone(brochureVO.getPhone());
            result.setEduEnrollmenBrochureVO(brochureVO);
            if(StringUtils.isNotBlank(brochureVO.getEnrollArea())){
                areaEnrollList=Arrays.stream(brochureVO.getEnrollArea().split(",")).map(s -> s).collect(Collectors.toList());
            }
            if (StringUtils.isNotBlank(brochureVO.getAdmissionsCategory())){
                List<OtherAreaConfigNameVO> configNameVOList= JSON.parseArray(brochureVO.getAdmissionsCategory(),OtherAreaConfigNameVO.class);
                result.setEnrollAreaConfigList(configNameVOList);
            }else{
                //为空则不控制
                result.setEnrollAreaConfigList(new ArrayList<>());
            }
        }
        if(StringUtils.isNotBlank(result.getAreaCode())&&!areaEnrollList.contains(result.getAreaCode())){
            areaEnrollList.add(result.getAreaCode());
        }

        result.setAreaEnrollList(areaEnrollList);

        if(result.getHasEnrollFlag()) {
            //获取表单配置
            QueryWrapper<EduFormEnroll> eduFormEnrollQueryWrapper = new QueryWrapper<>();
            eduFormEnrollQueryWrapper
                    .lambda()
                    .select(EduFormEnroll::getFormId,EduFormEnroll::getConfigName,EduFormEnroll::getConfigType,EduFormEnroll::getConfigTypeName,EduFormEnroll::getOrderNo,EduFormEnroll::getFormName,EduFormEnroll::getStatus,EduFormEnroll::getValidStatus)
                    .eq(EduFormEnroll::getSchoolId, orgId)
                    .eq(EduFormEnroll::getFormRefId, brochure.getEnrollmenBrochureId())
                    .eq(EduFormEnroll::getStatus, ArticleStatusEnum.on.getType())
                    .eq(EduFormEnroll::getValidStatus,BoolEnum.TRUE.getType())
                    .eq(EduFormEnroll::getYear, year)
                    .orderByAsc(EduFormEnroll::getOrderNo);
            List<EduFormEnroll> eduFormEnrollList = eduFormEnrollService.list(eduFormEnrollQueryWrapper);
            if (CollectionUtils.isEmpty(eduFormEnrollList)) {
                result.setFormList(new ArrayList<>());
            }else{
                result.setFormList(Convert.toList(EduFormEnrollVO.class,eduFormEnrollList));
                QueryWrapper<EduFormEnrollParams> eduFormEnrollParamsQueryWrapper = new QueryWrapper<>();
                eduFormEnrollParamsQueryWrapper
                        .lambda().in(EduFormEnrollParams::getFormId,eduFormEnrollList.stream().map(EduFormEnroll::getFormId).distinct().collect(Collectors.toList()))
                        .orderByAsc(EduFormEnrollParams::getOrderNo);
                List<EduFormEnrollParams> list=eduFormEnrollParamsService.list(eduFormEnrollParamsQueryWrapper);
                if(!CollectionUtils.isEmpty(list)){
                    List<EduFormEnrollParamsVO> vos=Convert.toList(EduFormEnrollParamsVO.class,list);
                    result.setFormParams(vos.stream().collect(Collectors.groupingBy(EduFormEnrollParamsVO::getFormId)));
                }else{
                    result.setFormParams(new HashMap<>());
                }
            }
            //获取特殊通道配置信息
            if(StringUtils.isNotBlank(brochure.getSpecialIds())){
                List<String> formIdList=Arrays.stream(brochure.getSpecialIds().split(",")).map(s -> s).collect(Collectors.toList());
                QueryWrapper<EduFormSpecial> eduFormSpecialQueryWrapper = new QueryWrapper<>();
                eduFormSpecialQueryWrapper
                        .lambda()
                        .select(EduFormSpecial::getFormSpecialId,EduFormSpecial::getBigClassKey,EduFormSpecial::getBigClassValue,EduFormSpecial::getSmallClassKey,EduFormSpecial::getSmallClassValue)
                        .eq(EduFormSpecial::getStatus, BoolEnum.TRUE.getType())
                        .eq(EduFormSpecial::getYear,brochure.getYear())
                        .eq(EduFormSpecial::getAreaCode, entity.getAreaCode())
                        .in(EduFormSpecial::getFormSpecialId,formIdList)
                        .orderByAsc(EduFormSpecial::getOrderNo);
                List<EduFormSpecial> specialList=eduFormSpecialService.list(eduFormSpecialQueryWrapper);
                if(!CollectionUtils.isEmpty(specialList)){
                    //大类设置
                    List<EduSchoolConfigVO> configSpecialList=new ArrayList<>();
                    //小类设置
                    List<EduFormEnrollVO> formSpecialList=new ArrayList<>();
                    for(EduFormSpecial eduFormSpecial:specialList){
                        EduFormEnrollVO eduFormEnrollVO=new EduFormEnrollVO();
                        eduFormEnrollVO.setFormId(eduFormSpecial.getFormSpecialId());
                        eduFormEnrollVO.setConfigTypeName(eduFormSpecial.getBigClassValue());
                        eduFormEnrollVO.setFormName(eduFormSpecial.getSmallClassValue());
                        eduFormEnrollVO.setValidStatus(eduFormSpecial.getStatus());
                        eduFormEnrollVO.setStatus(EnrollStatusEnum.on.getType());
                        eduFormEnrollVO.setConfigName(eduFormSpecial.getSmallClassValue());
                        eduFormEnrollVO.setOrderNo(99);
                        eduFormEnrollVO.setMbFlag(eduFormSpecial.getMbFlag());
                        eduFormEnrollVO.setMzgFlag(eduFormSpecial.getMzgFlag());
                        eduFormEnrollVO.setHouseFlag(eduFormSpecial.getHouseFlag());
                        eduFormEnrollVO.setFileFlag(eduFormSpecial.getFileFlag());
                        formSpecialList.add(eduFormEnrollVO);

                        EduSchoolConfigVO eduSchoolConfigVO=new EduSchoolConfigVO();
                        boolean flag=true;
                        for(EduSchoolConfigVO configVO:configSpecialList){
                            if(configVO.getConfigType().equalsIgnoreCase(eduFormSpecial.getBigClassKey().toString())){
                                flag=false;
                                break;
                            }
                        }
                        if(flag){
                            eduSchoolConfigVO.setConfigType(eduFormSpecial.getBigClassKey().toString());
                            eduSchoolConfigVO.setConfigTypeName(eduFormSpecial.getBigClassValue());
                            eduSchoolConfigVO.setStatus(BoolEnum.TRUE.getType());
                            configSpecialList.add(eduSchoolConfigVO);
                        }
                    }
                    result.setConfigSpecialList(configSpecialList);
                    result.setFormSpecialList(formSpecialList);
                    //表单配置
                    QueryWrapper<EduFormSpecialParams> eduFormSpecialParamsQueryWrapper = new QueryWrapper<>();
                    eduFormSpecialParamsQueryWrapper
                            .lambda().in(EduFormSpecialParams::getFormSpecialId,specialList.stream().map(EduFormSpecial::getFormSpecialId).distinct().collect(Collectors.toList()))
                            .orderByAsc(EduFormSpecialParams::getOrderNo);
                    List<EduFormSpecialParams> list=eduFormSpecialParamsService.list(eduFormSpecialParamsQueryWrapper);
                    if(!CollectionUtils.isEmpty(list)){
                        List<EduFormEnrollParamsVO> vos=Convert.toList(EduFormEnrollParamsVO.class,list);
                        result.setFormSpecialParams(vos.stream().collect(Collectors.groupingBy(EduFormEnrollParamsVO::getFormId)));
                    }else{
                        result.setFormSpecialParams(new HashMap<>());
                    }
                }else{
                    result.setFormSpecialParams(new HashMap<>());
                }
            }
        }
        //获取招生计划
        QueryWrapper<EduSchoolConfig> schoolConfigQueryWrapper = new QueryWrapper<>();
        schoolConfigQueryWrapper
                .lambda()
                .eq(EduSchoolConfig::getSchoolId, orgId)
                .eq(EduSchoolConfig::getStatus, BoolEnum.TRUE.getType())
                .eq(EduSchoolConfig::getYear, year);
        List<EduSchoolConfigVO> configVOList =
                Convert.toList(
                        EduSchoolConfigVO.class,
                        eduSchoolConfigService.list(schoolConfigQueryWrapper));
        dictHelper.valueToName(
                configVOList, Arrays.asList(SchoolConfigDictEnum.values()));
        result.setConfigList(configVOList);
        //突破学区
        LambdaQueryWrapper<EduBreakThrough> breakThroughLambdaQueryWrapper = new LambdaQueryWrapper<>();
        breakThroughLambdaQueryWrapper.select(EduBreakThrough::getBreakThroughId).eq(EduBreakThrough::getSchoolId, result.getOrgId())
                .eq(EduBreakThrough::getYear, year).eq(EduBreakThrough::getStatus, BoolEnum.TRUE.getType());
        List<EduBreakThrough> breakThroughList = eduBreakThroughService.list(breakThroughLambdaQueryWrapper);
        List<String> schoolIdList=new ArrayList<>();
        if (!CollectionUtils.isEmpty(breakThroughList)) {
            LambdaQueryWrapper<EduBreakThroughDetail> breakThroughDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            breakThroughDetailLambdaQueryWrapper.select(EduBreakThroughDetail::getSchoolId).eq(EduBreakThroughDetail::getBreakThroughId, breakThroughList.get(0).getBreakThroughId());

            List<EduBreakThroughDetail> detailList = eduBreakThroughDetailService.list(breakThroughDetailLambdaQueryWrapper);
            if (!CollectionUtils.isEmpty(detailList)) {
                schoolIdList = detailList.stream().map(EduBreakThroughDetail::getSchoolId).distinct().collect(Collectors.toList());
            }
        }
        schoolIdList.add(result.getOrgId());
        result.setBreakSchoolList(schoolIdList);

        return result;
    }

    @Override
    @Cached(name = EduCache.EDU_SCHOOL_areaCode, key = "#areaCode", cacheType = CacheType.BOTH, expire = 86400, localExpire = 20)
    public List<EduSchoolVO> getAppSchoolByArea(List<String> areaCodeList) {
        Integer year =eduHelper.thisYear();
        QueryWrapper<EduSchool> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().in(EduSchool::getAreaCode,areaCodeList)
                .eq(EduSchool::getStatus,BoolEnum.TRUE.getType())
                .eq(EduSchool::getRecruitStatus,BoolEnum.FALSE.getType())
        .orderByAsc(EduSchool::getOrderBy,EduSchool::getSchoolCategory,EduSchool::getOrgId);
        List<EduSchool> list=this.list(queryWrapper);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        List<String> orgIdList =
                list.stream().map(EduSchool::getOrgId).collect(Collectors.toList());
        //获取城区E学校信息
        QueryWrapper<EduAppointSchool> appointSchoolQueryWrapper = new QueryWrapper<>();
        appointSchoolQueryWrapper.lambda().in(EduAppointSchool::getNewSchoolId, orgIdList)
                .eq(EduAppointSchool::getStatus, BoolEnum.TRUE.getType())
                .eq(EduAppointSchool::getYear, year);
        List<EduAppointSchool> eduAppointSchoolList = eduAppointSchoolService.list(appointSchoolQueryWrapper);
        Map<String, String> appiontMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(eduAppointSchoolList)) {
            appiontMap = eduAppointSchoolList.stream().collect(Collectors.toMap(EduAppointSchool::getNewSchoolId, EduAppointSchool::getOldSchoolId, (k1, k2) -> k1));
        }
        //获取招生方案信息
        QueryWrapper<EduEnrollmenBrochure> brochureQueryWrapper = new QueryWrapper<>();
        brochureQueryWrapper.lambda().select(EduEnrollmenBrochure::getSchoolId,EduEnrollmenBrochure::getArticleId).in(EduEnrollmenBrochure::getSchoolId, orgIdList)
                .eq(EduEnrollmenBrochure::getYear, year);
        List<EduEnrollmenBrochure> brochureList =
                eduEnrollmenBrochureService.list(brochureQueryWrapper);

        Map<String, Long> brochureMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(brochureList)) {
            List<Long> articleIdList =
                    brochureList.stream().map(EduEnrollmenBrochure::getArticleId).collect(Collectors.toList());
            QueryWrapper<AppArticle> articleQueryWrapper=new QueryWrapper<>();
            articleQueryWrapper.lambda().select(AppArticle::getOrgId,AppArticle::getArticleId).in(AppArticle::getArticleId,articleIdList)
            .eq(AppArticle::getArticleStatus,ArticleStatusEnum.on.getType());
            List<AppArticle> appArticleList=appArticleService.list(articleQueryWrapper);
            if(!CollectionUtils.isEmpty(appArticleList)){
                brochureMap = appArticleList.stream().collect(Collectors.toMap(AppArticle::getOrgId, AppArticle::getArticleId, (k1, k2) -> k1));
            }
        }
        //招生计划信息
        QueryWrapper<EduSchoolConfig> schoolConfigQueryWrapper = new QueryWrapper<>();
        schoolConfigQueryWrapper
                .lambda()
                .in(EduSchoolConfig::getSchoolId, orgIdList)
                .eq(EduSchoolConfig::getStatus, BoolEnum.TRUE.getType())
                .eq(EduSchoolConfig::getYear, year);
        List<EduSchoolConfigVO> allConfigList =
                Convert.toList(
                        EduSchoolConfigVO.class,
                        eduSchoolConfigService.list(schoolConfigQueryWrapper));
        Map<String, List<EduSchoolConfigVO>> configGroupBy = new HashMap<>();
        if (!CollectionUtils.isEmpty(allConfigList)) {
            configGroupBy = allConfigList.stream().collect(Collectors.groupingBy(EduSchoolConfigVO::getSchoolId));
        }
        //是否补报
        QueryWrapper<EduSchoolBb> bbQueryWrapper=new QueryWrapper<>();
        bbQueryWrapper.lambda().select(EduSchoolBb::getSchoolId,EduSchoolBb::getFullFlag).eq(EduSchoolBb::getYear,year)
                .in(EduSchoolBb::getSchoolId,orgIdList);
        List<EduSchoolBb> bbList=eduSchoolBbService.list(bbQueryWrapper);
        Map<String, String> bbMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(bbList)) {
            bbMap = bbList.stream().collect(Collectors.toMap(EduSchoolBb::getSchoolId, EduSchoolBb::getFullFlag, (k1, k2) -> k1));
        }
        //是否抢报
        QueryWrapper<EduSchoolVacancy> vacancyQueryWrapper=new QueryWrapper<>();
        vacancyQueryWrapper.lambda().select(EduSchoolVacancy::getSchoolId).eq(EduSchoolVacancy::getYear,year)
                .in(EduSchoolVacancy::getSchoolId, orgIdList)
                .gt(EduSchoolVacancy::getVacancyNum, 0);
        List<EduSchoolVacancy> qbList=eduSchoolVacancyService.list(vacancyQueryWrapper);
        Map<String, String> qbMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(qbList)) {
            qbMap = qbList.stream().collect(Collectors.toMap(EduSchoolVacancy::getSchoolId, EduSchoolVacancy::getSchoolId, (k1, k2) -> k1));
        }
        List<EduSchoolVO> result=new ArrayList<>();
        for(EduSchool eduSchool:list){
            EduSchoolVO eduSchoolVO=Convert.convert(EduSchoolVO.class,eduSchool);
            if (appiontMap.containsKey(eduSchoolVO.getOrgId())) {
                eduSchoolVO.setAppointFlag(true);
            } else {
                eduSchoolVO.setAppointFlag(false);
            }
            if (brochureMap.containsKey(eduSchoolVO.getOrgId())) {
                eduSchoolVO.setHasEnrollFlag(true);
            } else {
                eduSchoolVO.setHasEnrollFlag(false);
            }
            if (bbMap.containsKey(eduSchoolVO.getOrgId())) {
                eduSchoolVO.setBbFlag(BoolEnum.TRUE.getType());
                eduSchoolVO.setFullFlag(bbMap.get(eduSchoolVO.getOrgId()));
            } else {
                eduSchoolVO.setBbFlag(BoolEnum.FALSE.getType());
            }
            if (qbMap.containsKey(eduSchoolVO.getOrgId())) {
                eduSchoolVO.setQbFlag(BoolEnum.TRUE.getType());
            } else {
                eduSchoolVO.setQbFlag(BoolEnum.FALSE.getType());
            }
            List<EduSchoolConfigVO> configList =
                    configGroupBy.get(eduSchoolVO.getOrgId());
            eduSchoolVO.setConfigList(configList);
            result.add(eduSchoolVO);
        }
        dictHelper.valueToName(result, Arrays.asList(SchoolDictEnum.values()));
        return result;
    }

    @Override
    public void clearCache(String orgId, String areaCode) {
        if (StringUtils.isNotBlank(orgId)) {
            Cache cache = JetCacheUtils.create(EduCache.EDU_SCHOOL_DETAIL, CacheType.BOTH);
            if (cache != null) {
                cache.remove(orgId);
            }
        }
        if (StringUtils.isNotBlank(orgId)) {
            Cache cache = JetCacheUtils.create(EduCache.EDU_ENROLL_FORM_SCHOOL, CacheType.BOTH);
            if (cache != null) {
                cache.remove(orgId);
            }
        }
        if (StringUtils.isNotBlank(areaCode)) {
            Cache cache = JetCacheUtils.create(EduCache.EDU_SCHOOL_areaCode, CacheType.BOTH);
            if (cache != null) {
                cache.remove(areaCode);
            }
        }
    }
}
